package com.example.pearl.common.tree;

import java.util.ArrayList;
import java.util.List;

public class TreeUtilsDemo {

    public static void main(String[] args) {
        // 构造TreeNode对象的list数据
        List<TreeNode> nodeList = new ArrayList<>();
        nodeList.add(new TreeNode(1, 0, "根节点1"));
        nodeList.add(new TreeNode(2, 0, "根节点2"));
        nodeList.add(new TreeNode(3, 1, "子节点1-1"));
        nodeList.add(new TreeNode(4, 1, "子节点1-2"));
        nodeList.add(new TreeNode(5, 2, "子节点2-1"));
        nodeList.add(new TreeNode(6, 3, "孙节点1-1-1"));
        nodeList.add(new TreeNode(7, 3, "孙节点1-1-2"));
        
        // 验证TreeUtil的方法
        List<TreeNode> tree = TreeUtils.buildTree(nodeList);
        
        // 输出结果验证
        System.out.println("构建的树形结构:");
        printTree(tree, 0);
    }
    
    // 辅助方法：打印树形结构
    private static void printTree(List<TreeNode> nodes, int depth) {
        for (TreeNode node : nodes) {
            for (int i = 0; i < depth; i++) {
                System.out.print("  ");
            }
            System.out.println(node.getName());
            if (node.getChildren() != null && !node.getChildren().isEmpty()) {
                printTree(node.getChildren(), depth + 1);
            }
        }
    }


}